% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

% This file is a template using the "beamer" package to create slides for a talk or presentation
% - Talk at a conference/colloquium.
% - Talk length is about 20min.
% - Style is ornate.

% MODIFIED by Jonathan Kew, 2008-07-06
% The header comments and encoding in this file were modified for inclusion with TeXworks.
% The content is otherwise unchanged from the original distributed with the beamer package.

\documentclass{beamer}


% Copyright 2004 by Till Tantau <tantau@users.sourceforge.net>.
%
% In principle, this file can be redistributed and/or modified under
% the terms of the GNU Public License, version 2.
%
% However, this file is supposed to be a template to be modified
% for your own needs. For this reason, if you use this file as a
% template and not specifically distribute it as part of a another
% package/program, I grant the extra permission to freely copy and
% modify this file as you see fit and even to delete this copyright
% notice. 


\mode<presentation>
{
  \usetheme{Warsaw}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}


\usepackage[english]{babel}
% or whatever

\usepackage[utf8]{inputenc}
% or whatever

\usepackage{times}
\usepackage[T1]{fontenc}
% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.


\title[Mongoose -- an object document model] % (optional, use only with long paper titles)
{Mongoose -- an object document model}


\author[Zhilei Ren] % (optional, use only with lots of authors)
{Zhilei Ren}
% - Give the names in the same order as the appear in the paper.
% - Use the \inst{?} command only if the authors have different
%   affiliation.

% This is only inserted into the PDF information catalog. Can be left
% out. 



% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

% \pgfdeclareimage[height=0.5cm]{university-logo}{university-logo-filename}
% \logo{\pgfuseimage{university-logo}}



% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:


% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command: 

%\beamerdefaultoverlayspecification{<+->}


\begin{document}

\begin{frame}
  \titlepage
\end{frame}



% Structuring a talk is a difficult task and the following structure
% may not be suitable. Here are some rules that apply for this
% solution: 

% - Exactly two or three sections (other than the summary).
% - At *most* three subsections per section.
% - Talk about 30s to 2min per frame. So there should be between about
%   15 and 30 frames, all told.

% - A conference audience is likely to know very little of what you
%   are going to talk about. So *simplify*!
% - In a 20min talk, getting the main ideas across is hard
%   enough. Leave out details, even if it means being less precise than
%   you think necessary.
% - If you omit details that are vital to the proof/implementation,
%   just say so once. Everybody will be happy with that.


\begin{frame}
	\begin{block}{Installing with npm}
		Install Mongoose from the command line using npm:
	\end{block}
	\begin{block}
	\scriptsize

		\$npm install mongoose
	\end{block}

	\begin{block}{Requirements}
	Now say we like fuzzy kittens and want to record every kitten we ever meet in MongoDB. The first thing we need to do is include mongoose in our project and open a connection to the test database on our locally running instance of MongoDB.

	\end{block}
	\begin{block}
	\scriptsize

	var mongoose = require('mongoose');

	mongoose.connect('mongodb://localhost/test');
	\end{block}
\end{frame}


\begin{frame}
	\begin{block}{Opening connection}
We have a pending connection to the test database running on localhost. We now need to get notified if we connect successfully or if a connection error occurs:

	\end{block}
	\begin{block}
	\scriptsize
var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function (callback) \{

\ \   //yay!

\});
\end{block}
\end{frame}

\begin{frame}
	\begin{block}{Construct the schema}
Once our connection opens, our callback will be called. For brevity, let's assume that all following code is within this callback.

With Mongoose, everything is derived from a Schema. Let's get a reference to it and define our kittens.

	\end{block}
	\begin{block}
	\scriptsize
var kittySchema = mongoose.Schema(\{

    name: String

\});

\end{block}
\end{frame}
\begin{frame}
	\begin{block}{Adding attributes}
So far so good. We've got a schema with one property, name, which will be a String. The next step is compiling our schema into a Model.

	\end{block}
	\begin{block}
	\scriptsize
var Kitten = mongoose.model('Kitten', kittySchema);

	\end{block}
	\begin{block}{Instantiation}
A model is a class with which we construct documents. In this case, each document will be a kitten with properties and behaviors as declared in our schema. Let's create a kitten document representing the little guy we just met on the sidewalk outside:

	\end{block}
	\begin{block}
	\scriptsize
var silence = new Kitten(\{ name: 'Silence' \});

console.log(silence.name); // 'Silence'

\end{block}
\end{frame}
\begin{frame}
	\begin{block}{Add methods}
Kittens can meow, so let's take a look at how to add "speak" functionality to our documents:

	\end{block}
	\begin{block}
	\scriptsize
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () \{
  var greeting = this.name
    ? "Meow name is " + this.name
    : "I don't have a name";
  console.log(greeting);
\}

var Kitten = mongoose.model('Kitten', kittySchema);

\end{block}
\end{frame}
\begin{frame}
	\begin{block}{Method calling}
Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance:

	\end{block}
	\begin{block}
	\scriptsize
var fluffy = new Kitten(\{ name: 'fluffy' \});

fluffy.speak(); // "Meow name is fluffy"
\end{block}
\end{frame}
\begin{frame}
	\begin{block}{Saving the instance}

We have talking kittens! But we still haven't saved anything to MongoDB. Each document can be saved to the database by calling its save method. The first argument to the callback will be an error if any occurred.

	\end{block}
	\begin{block}
	\scriptsize
fluffy.save(function (err, fluffy) \{

\ \ if (err) return console.error(err);

\ \ fluffy.speak();

\});

\end{block}
\end{frame}
\begin{frame}
	\begin{block}{Query}
Say time goes by and we want to display all the kittens we've seen. We can access all of the kitten documents through our Kitten model.

	\end{block}
	\begin{block}
	\scriptsize
Kitten.find(function (err, kittens) \{

\ \ if (err) return console.error(err);

\ \ console.log(kittens);

\})


\end{block}
\end{frame}
\begin{frame}
	\begin{block}{Filtering}
We just logged all of the kittens in our db to the console. If we want to filter our kittens by name, Mongoose supports MongoDBs rich querying syntax.

	\end{block}
	\begin{block}
	\scriptsize
Kitten.find(\{ name: /Fluff/ \});

\end{block}
\end{frame}
\end{document}


